package org.example.system.controller;


import org.example.commons.controller.BaseController;
import org.example.commons.entity.SysAdmin;
import org.example.commons.entity.SysFeedback;
import org.example.commons.entity.SysUser;
import org.example.commons.utils.SortUtils;
import org.example.commons.vo.ResultVO;
import org.example.system.service.ISysFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 用户反馈 控制层
 *
 * @author bothin
 */
@Secured({"ROLE_SUPER_ADMIN"})
@RestController
@RequestMapping("/api/system/feedback")
public class SysFeedbackController extends BaseController {
    @Autowired
    private ISysFeedbackService feedbackService;

    /**
     * 导出excel
     * @param response
     * @throws IOException
     */
    @GetMapping("/export/excel")
    public void exportExcel(@RequestParam(name = "ids", required = false)  Long[] ids,
                            HttpServletResponse response) throws IOException {
        List<SysFeedback> data = null;
        if (ids==null || ids.length==0){
            data = feedbackService.queryAll();
        }else {
            data = feedbackService.findByFeedbackIdIn(ids);
        }
        super.exportExcel(response,data, SysFeedback.class,"用户反馈数据表");
    }

    @GetMapping("/{id}")
    public ResultVO findOne(@PathVariable("id") Long id) {
        return ResultVO.success(feedbackService.queryById(id));
    }

    /**
     * 查询反馈记录列表
     *
     * @param page  页数
     * @param size  每页大小
     * @param isAsc 是否正序排序
     * @param field 排序的字段
     * @return ResultVO
     */
    @GetMapping
    public ResultVO findList(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "size", defaultValue = "10") Integer size,
                             @RequestParam(name = "isAsc", defaultValue = "false") Boolean isAsc,
                             @RequestParam(name = "field", defaultValue = "createTime") String field) {
        //排序
        Sort sort = SortUtils.getSort(isAsc, field);
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        Page<SysFeedback> admins = feedbackService.queryAllPage(pageRequest);
        return ResultVO.success(admins);
    }

    /**
     * 条件查询列表
     *
     * @param page  页数
     * @param size  每页大小
     * @param isAsc 是否正序排序
     * @param field 排序的字段
     * @return ResultVO
     */
    @GetMapping("/search")
    public ResultVO searchList(@RequestParam(name = "userName", defaultValue = "") String name,
                               @RequestParam(name = "content", defaultValue = "")  String content,
                               @RequestParam(name = "dates", required = false) Date[] dates,
                               @RequestParam(name = "page", defaultValue = "1") Integer page,
                               @RequestParam(name = "size", defaultValue = "10") Integer size,
                               @RequestParam(name = "isAsc", defaultValue = "true") Boolean isAsc,
                               @RequestParam(name = "field", defaultValue = "") String field) {
        PageRequest pageRequest = null;
        if (StringUtils.isEmpty(field)) {
            pageRequest = PageRequest.of(page - 1, size);
        } else {
            //排序
            Sort sort = SortUtils.getSort(isAsc, field);
            pageRequest = PageRequest.of(page - 1, size, sort);
        }

        if (dates==null || dates.length<2){
            dates = new Date[2];
        }

        Page<SysFeedback> res = feedbackService.searchList(name, content, dates, pageRequest);
        return ResultVO.success(res);
    }


    /**
     * 更新或保存反馈记录
     *
     * @param feedback 反馈记录
     * @return ResultVO
     */
    @Secured({"ROLE_SUPER_ADMIN","ROLE_ADMIN","ROLE_CHECK_MAN","ROLE_SYS_USER"})
    @PostMapping
    public ResultVO saveSysAdmin(@RequestBody SysFeedback feedback) {
        return ResultVO.success(feedbackService.insert(feedback));
    }

    @DeleteMapping
    public ResultVO deleteSysAdmin(@RequestParam("id") Long id) {
        feedbackService.deleteById(id);
        return ResultVO.success();
    }

    @DeleteMapping("/list")
    @Transactional
    public ResultVO deleteSysAdmin(@RequestParam("ids") Long[] ids) {
        for (Long id : ids) {
            feedbackService.deleteById(id);
        }
        return ResultVO.success();
    }
}
